<?php
/*
 * Created on 01-Aug-2006
 *
 * program called from view.php to create a xml feed type file
 * and allow it to be downloaded
 * 
 * author Alan Carter
 * 
 */

// resolves bug #1509

global $DISABLESAMS;
$DISABLESAMS = true;

require_once("module_resourcepage.php");
require_once("resourcepage_container.php");
require_once("RSSexport.php");

require_once(dirname(__FILE__).'/../../lib/magpie/rss_fetch.inc');
require_once(dirname(__FILE__).'/../../lib/magpie/rss_cache.inc');


// Defines for moodle's use of magpierss classes
define('MAGPIE_PROXY_HOST', $CFG->proxyhost);   // Could be empty, that's OK
define('MAGPIE_PROXY_PORT', $CFG->proxyport);   // Could be empty, that's OK
// define('MAGPIE_DIR', $CFG->dirroot.'/lib/magpie/');
define('MAGPIE_CACHE_DIR', $CFG->dataroot .'/cache/rsscache');
define('MAGPIE_CACHE_ON', true); //might want to expose as an admin config option, but perhaps this is something that should truly just be on unless the code is tweaked
define('MAGPIE_CACHE_FRESH_ONLY', false); //should be exposed as an admin config option
// define('MAGPIE_CACHE_AGE', $CFG->block_rss_timeout);

/**
if ($CFG->debug) {
    define('MAGPIE_DEBUG', $CFG->debug); // magpie, like moodle, takes an integer debug
} else {
        
    define('MAGPIE_DEBUG', 0); // 0 is DEBUG off for magpie
}

*/

define('MAGPIE_DEBUG', 1); // 1 is DEBUG on for magpie
define('MAGPIE_CACHE_AGE', 3600);


	/*
	 * creates a directory off of moodle data and places the content into a xml file
	 * @param id - not used
	 * @param rpid - resourcepage id
	 * @param content to be stored in the file created
	 * @param feedtype - type of xml feed i.i rss or atom
	 * @return directory and filename where the xml file is stored
	 */
    function createdir($id, $rpid, $content, $feedtype) {
    
        global $CFG;

        $cachedir = $CFG->dataroot.'/cache/'.$feedtype;
        $libdir =  $CFG->libdir;

        // save content to a directory

        $cache = new RSSexport($cachedir, MAGPIE_CACHE_AGE);
       
        if (MAGPIE_DEBUG and $cache->ERROR) {
            do_debug($cache->ERROR, E_USER_WARNING);
        }

		// dummy code for creating a rss feed url to the xml file 
        // $dataurl = $CFG->wwwdataroot;
        // $urlcache = $dataurl.'/cache/'.$feedtype;
        // $urlcachefile = $urlcache.'/'.$rpid.'.xml';
        // return $urlcachefile;
 
        $cachefile = $cachedir.'/'.$rpid.'.xml';

        $cache->set($cachefile, $content);
       
        return $cachefile;

    }


    /*
     * Download xml
     * @param string filename i.e. nnn.xml
     * @param string full directory and file name
     * @param boolean download should be set to true
     * @return None 
     */
      function download($filename, $filenamedir, $download) {

        //send headers
        
        
        
        if(!$download)
        {
            //display inside browser (after delimiting with tabs)
            header("Content-type: application/xml");
            header("Content-disposition: filename=\"".$filename."\"\n");
        } else {
            //force download dialog
            // $filename = $courseidnumber.'.csv';
            if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
                // $filename = preg_replace('/\./', '%2e', $filename, substr_count($filename, '.') - 1);
            }
            header("Content-type: application/force-download\n");
            header("Content-disposition: attachment; filename=\"".$filename."\"\n");
        }
        header("Content-transfer-encoding: binary\n");
        header("Content-length: " . filesize($filenamedir) . "\n");
        header("Cache-Control: "); // Required for IE Open
        header("Pragma: "); // Required for IE Open

        //send file contents
        $fp=fopen($filenamedir, "r");
        fpassthru($fp);
        fclose($fp);

        // Deletefile (ignore any errors)
        @unlink($filenamedir);
        
    }


	global $DISABLESAMS;
	$DISABLESAMS = true;

	// main code
	
	$module=module_resourcepage::$inst;
 
	$id=optional_param('id',0,PARAM_INT);    // Course Module ID, or
	$a=optional_param('a',0,PARAM_INT);     // item ID        
	$item=optional_param('item',0,PARAM_ALPHANUM);  // Resourcepage editing item id
	$feedtype=optional_param('feedtype',0,PARAM_ALPHANUM); 

	$module->obtain_view_info($id,$a,$cm,$course,$resourcepage);
	// $module->verify_view($cm,$course);
  	
	add_to_log($course->id, "creating rss feed", "view", "view.php?id=$cm->id", "$resourcepage->id",$cm->id);
 
 	$content=$module->get_container_items($resourcepage->containerid);
   	$item=false; // Can't be editing single item either thanks
    
    $feed = '';
    $editing = true;
    $download = false;
	
	if($feedtype == 'rss') {
		
 		$feed = $content[0]->export_as_rss($course,$resourcepage,$cm,$editing,$item);
 		
	} else {
		
		if($feedtype == 'rssfeed') {
		
 			$feed = $content[0]->export_as_rss($course,$resourcepage,$cm,$editing,$item);
			$download = false;
		} 
		//ou-specific begins
		else if($feedtype == 'podcastrss'){
			
			$feed = $content[0]->export_as_podcastrss($course,$resourcepage,$cm);
			
		}
		//ou-specific ends
		else {
			
			// assume to be atom
		
			$feed = $content[0]->export_as_atom($course,$cm,$editing,$item);
		
		} // endif
		
	} // endif


    $cachefile = createdir($id, $resourcepage->id, $feed, $feedtype);

 	// $download = true;
	$filename = $resourcepage->id.'.xml';

	download($filename, $cachefile, $download);


?>
